<template>
  <div class="app-container">
    <el-form
      ref="listFilterRef"
      :model="list.filter"
      inline
      class="list-filter"
    >
      <el-form-item prop="popupName">
        <el-input
          v-model="list.filter.popupName"
          placeholder="弹窗搜索"
          clearable
        />
      </el-form-item>
      <el-form-item prop="areaCode">
        <RegionSelect
          v-model="list.filter.areaCode"
          :props="{ checkStrictly: true }"
        />
      </el-form-item>

      <el-form-item prop="status">
        <Status v-model="list.filter.status" />
      </el-form-item>

      <el-form-item prop="effectiveStatus">
        <el-select
          v-model="list.filter.effectiveStatus"
          placeholder="过期状态"
          clearable
        >
          <el-option label="未过期" :value="0"></el-option>
          <el-option label="已过期" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="reset">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <div class="table-operation">
      <AuthButton @click="c" name="新增" />
      <el-pagination
        v-bind="elPaginationProps"
        :total="list.total"
        :currentPage.sync="list.filter.pageNo"
        :pageSize.sync="list.filter.pageSize"
      />
    </div>

    <el-table v-loading="list.loading" :data="list.data" v-bind="tableProp">
      <el-table-column label="所属地区" prop="areaName" />
      <el-table-column label="弹窗名称" prop="popupName" />
      <el-table-column label="弹窗图片">
        <template slot-scope="scope">
          <PicViewer :value="scope.row.popupImg" tableCell />
        </template>
      </el-table-column>
      <el-table-column label="生效时段">
        <template slot-scope="{ row }">
          {{ `${row.effectiveBeginTime} 至 ${row.effectiveEndTime}` }}
          <el-tag v-if="row.effectiveStatus" type="danger">已过期</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="跳转类型" prop="type">
        <template slot-scope="{ row }">
          {{ row.type | $value2label(dict.link_type) }}
        </template>
      </el-table-column>
      <el-table-column label="跳转项名称" prop="srcName" />
      <el-table-column label="启用状态" width="80" align="center">
        <template slot-scope="{ row: { id, status } }">
          <KiPopSwitch
            v-bind="popSwitchProps(status)"
            @change="updateStatus({ id, status: status ^ 1 })"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="{ row }">
          <AuthButton @click="u({ id: row.id })" name="编辑" />
          <AuthButton @click="d({ id: row.id })" name="删除" />
        </template>
      </el-table-column>
    </el-table>

    <KiFormDialog
      :show.sync="form.show"
      :title="formTitle"
      v-model="form.data"
      :submit="submitForm"
      :readonly="form.status === 'r'"
      ref="formRef"
      :loading="form.loading"
    >
      <template #el-form>
        <el-form-item label="弹窗名称" prop="popupName" verify>
          <el-input
            v-model="form.data.popupName"
            clearable
            maxlength="30"
            show-word-limit
            placeholder="请输入弹窗名称"
          />
        </el-form-item>
        <el-form-item label="所属地区" prop="areaCodes" verify>
          <RegionSelect
            v-model="form.data.areaCodes"
            :props="{ multiple: true, checkStrictly: true }"
            :level="2"
          />
        </el-form-item>
        <el-form-item label="弹窗图片" prop="popupImg" verify>
          <Imgpond v-model="form.data.popupImg" :count="1" />
        </el-form-item>
        <el-form-item label="生效时段" prop="effectiveBeginTime" verify>
          <TimeRangePicker
            type="date-time"
            :start.sync="form.data.effectiveBeginTime"
            :end.sync="form.data.effectiveEndTime"
            placeholder="生效起期/生效止期"
            :pickerOptions="{
              startDisabledDate : (time, start, end) => {
                return time.getTime() < Date.now() - 8.64e7
              }
            }"
          />
        </el-form-item>
        <el-form-item label="跳转页面" prop="type">
          <GlobalSearch
            v-model="form.data.srcId"
            :type.sync="form.data.type"
            :sourceId.sync="form.data.srcId"
            :sourceName.sync="form.data.srcName"
          />
        </el-form-item>
      </template>
    </KiFormDialog>
  </div>
</template>

<script>
import pageMixin from '@/utils/pageMixin'
import useAdmateAdapter from '@/utils/useAdmateAdapter'

export default {
  mixins: [pageMixin],
  setup: () =>
    useAdmateAdapter({
      urlPrefix: 'one-travel-api/home-popup'
    })
}
</script>

<style lang="scss" scoped></style>
